package com.lepu.app.fun.my.bean;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;

import com.app.utils.DBHelper;
import com.core.lib.utils.main.UtilityBase;

public class BeanCollectKnowledge {

	private static final String TABLE_NAME = "CollectHistory";
	
	public static final int TYPE_VIDEO_OLD = 0;//没有type或者为0是视频
	public static final int TYPE_ARTICLE_OLD = 1;

	//=====================================================
	public static final int TYPE_QUESTION = 2;
	public static final int TYPE_TIEZI = 3;
	public static final int TYPE_ARTICLE_WRITER = 4;//作家专栏文章
	public static final int TYPE_ARTICLE_NEW = 5;
	public static final int TYPE_VIDEO_NEW = 6;

	public int Id;

	public String Uid;
	public String Name;//no use
	public int Type;//no use
	public long Time;
	public int VideoIndex;
	
	//new add
	public String Title;
	public String SubTitle;//
	public String ArticleID;//文章，和帖子共用id

	public static boolean insertCollectItem(BeanCollectKnowledge item) {
		boolean flag = false;

		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return flag;

		ContentValues values = new ContentValues();
		values.put("Uid", item.Uid);
		values.put("Name", item.Name);
		values.put("Type", item.Type);
		values.put("Time", item.Time);
		values.put("VideoIndex", item.VideoIndex);
		
		values.put("Title", item.Title);
		values.put("SubTitle", item.SubTitle);
		values.put("ArticleID", item.ArticleID);

		flag = dbHelp.insert(TABLE_NAME, null, values);
		
		return flag;
	}

	/*
	 * video
	 */
	public static boolean checkCollectItemVideo(String uid,int videoIndex) {
		boolean flag = false;
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return flag;

		String sql = "select * from CollectHistory where Uid=? and VideoIndex=? and Type=?";
		ArrayList<HashMap<String, Object>> queryResult = dbHelp.query(sql,
				new String[] { uid,String.valueOf(videoIndex),String.valueOf(TYPE_VIDEO_NEW) });

		if (queryResult != null && queryResult.size() > 0) {
			flag = true;
		}

		return flag;
	}
	
	public static boolean deleteItemVideo(String uid,int videoIndex) {
		boolean flag = false;

		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return flag;

		flag = dbHelp.delete(TABLE_NAME, "Uid=? and VideoIndex=? and Type=?",
				new String[] { uid,String.valueOf(videoIndex),String.valueOf(TYPE_VIDEO_NEW) });

		return flag;
	}
	
	public static boolean deleteCollectData(String uid,int type) {
		boolean flag = false;

		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return flag;

		flag = dbHelp.delete(TABLE_NAME, "Uid=? and Type=?",
				new String[] { uid,String.valueOf(type) });

		return flag;
	}

	public static boolean deleteCollectData(int type) {
		boolean flag = false;

		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return flag;

		flag = dbHelp.delete(TABLE_NAME, "Type=?",
				new String[] { String.valueOf(type) });

		return flag;
	}
	
	/*
	 * question
	 */
	public static boolean checkCollectItemQuestion(String uid,String Title) {
		boolean flag = false;
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return flag;

		String sql = "select * from CollectHistory where Uid=? and Title=? and Type=?";
		ArrayList<HashMap<String, Object>> queryResult = dbHelp.query(sql,
				new String[] { uid,String.valueOf(Title),String.valueOf(TYPE_QUESTION) });

		if (queryResult != null && queryResult.size() > 0) {
			flag = true;
		}

		return flag;
	}

	public static boolean deleteItemQuestion(String uid,String Title) {
		boolean flag = false;

		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return flag;

		flag = dbHelp.delete(TABLE_NAME, "Uid=? and Title=? and Type=?",
				new String[] { uid,String.valueOf(Title),String.valueOf(TYPE_QUESTION) });

		return flag;
	}
	
	/*
	 * article
	 */
	public static boolean checkCollectItemArticle(String uid,String ArticleID,int type) {
		boolean flag = false;
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return flag;

		String sql = "select * from CollectHistory where Uid=? and ArticleID=? and Type=?";
		ArrayList<HashMap<String, Object>> queryResult = dbHelp.query(sql,
				new String[] { uid,String.valueOf(ArticleID),String.valueOf(type) });

		if (queryResult != null && queryResult.size() > 0) {
			flag = true;
		}

		return flag;
	}

	public static boolean deleteItemArticle(String uid,String ArticleID,int type) {
		boolean flag = false;

		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return flag;

		flag = dbHelp.delete(TABLE_NAME, "Uid=? and ArticleID=? and Type=?",
				new String[] { uid,String.valueOf(ArticleID),String.valueOf(type) });

		return flag;
	}
	
	/*
	 * post
	 */
	public static boolean checkCollectItemPost(String uid,String Title) {
		boolean flag = false;
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return flag;

		String sql = "select * from CollectHistory where Uid=? and Title=? and Type=?";
		ArrayList<HashMap<String, Object>> queryResult = dbHelp.query(sql,
				new String[] { uid,String.valueOf(Title),String.valueOf(TYPE_TIEZI) });

		if (queryResult != null && queryResult.size() > 0) {
			flag = true;
		}

		return flag;
	}

	public static boolean deleteItemPost(String uid,String Title) {
		boolean flag = false;

		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return flag;

		flag = dbHelp.delete(TABLE_NAME, "Uid=? and Title=? and Type=?",
				new String[] { uid,String.valueOf(Title),String.valueOf(TYPE_TIEZI) });

		return flag;
	}

	/*
	 * 当前用户是否有自己的图片相册
	 */
	public static ArrayList<BeanCollectKnowledge> getAllCollect(String uid) {
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return null;

		String sql = "select * from CollectHistory where Uid=? order by Id desc";
		ArrayList<HashMap<String, Object>> queryResult = dbHelp.query(sql,
				new String[] {uid});

		if (queryResult == null || queryResult.size() == 0) {
			return null;
		}

		ArrayList<BeanCollectKnowledge> dataList = new ArrayList<BeanCollectKnowledge>();
		for (int arrayIndex = 0; arrayIndex < queryResult.size(); arrayIndex++) {
			HashMap<String, Object> dictionary = queryResult.get(arrayIndex);
			BeanCollectKnowledge item = BeanCollectKnowledge
					.parseFromDatabase(dictionary);
			dataList.add(item);
		}
		return dataList;
	}
	
	/*
	 * 当前用户所有的照片数量
	 */
	public static int getAllCollectCount(String uid) {
		
		int count = 0;
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return count;

		String sql = "select count(*) from CollectHistory where Uid=?";
		String result =  dbHelp.executeScalar(sql,new String[]{uid});
		count = UtilityBase.parseInt(result);

		return count;
	}

	private static BeanCollectKnowledge parseFromDatabase(
			HashMap<String, Object> dictionary) {

		if (dictionary == null || dictionary.size() == 0) {
			return null;
		}
		BeanCollectKnowledge item = new BeanCollectKnowledge();

		item.Id = UtilityBase.parseInt((String) dictionary.get("Id"));
		item.Uid = UtilityBase.parseString((String) dictionary.get("Uid"));
		item.Name = UtilityBase.parseString((String) dictionary.get("Name"));
		item.Type = UtilityBase.parseInt((String) dictionary.get("Type"));
		item.Time = UtilityBase.parseLong((String) dictionary.get("Time"));
		item.VideoIndex = UtilityBase.parseInt((String) dictionary
				.get("VideoIndex"));
		
		item.Title = UtilityBase.parseString((String) dictionary.get("Title"));
		item.SubTitle = UtilityBase.parseString((String) dictionary.get("SubTitle"));
		item.ArticleID = UtilityBase.parseString((String) dictionary.get("ArticleID"));

		return item;
	}
}
